import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { ReworkDocModel } from '#/api/rework/models';

import {
  useReworkModeOptions,
  useReworkResultOptions,
  useReworkStatusOptions,
} from '#/api/rework/models';

/** 获取表格查询表单的字段配置。 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'sn',
      label: '零件码',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'wo',
      label: '工单编号',
    },
    {
      component: 'RangePicker',
      fieldName: 'rangeDate',
      label: '返工日期',
    },
  ];
}

/**
 * 获取表格列配置。
 */
export function useColumns(): VxeTableGridOptions<ReworkDocModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'sn', title: '零件码', width: 220 },
    { field: 'wo', title: '工单编号', width: 140 },
    { field: 'productCode', title: '产品编号', width: 140 },
    { field: 'ngOperationCode', title: '不合格工序', width: 100 },
    { field: 'ngWorkstationCode', title: '不合格工位', width: 100 },
    {
      field: 'reworkMode',
      title: '返工模式',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useReworkModeOptions(),
      },
    },
    {
      field: 'targetOperationCode',
      title: '重新上线工序',
      width: 110,
      cellRender: {
        attrs: {
          color: 'orange',
        },
        name: 'CellTag',
      },
    },
    {
      field: 'defects',
      title: '缺陷问题',
      width: 160,
      formatter: ({ cellValue }) => {
        return cellValue.map((e: any) => e.defectName).join('、');
      },
    },
    { field: 'remark', title: '返工原因', minWidth: 100 },
    {
      field: 'status',
      title: '返工状态',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useReworkStatusOptions(),
      },
    },
    {
      field: 'result',
      title: '返工结果',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useReworkResultOptions(),
      },
    },
    { field: 'startTime', title: '开始时间', width: 150 },
    { field: 'endTime', title: '完成时间', width: 150 },
  ];
}
